{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 51\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Write a function to compute 5/0 and use try/except to catch the exceptions._**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints\n",
    "\n",
    "> **_Use try/except to catch exceptions._**\n",
    "\n",
    "---\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def divide():\n",
    "    return 5 / 0\n",
    "\n",
    "\n",
    "try:\n",
    "    divide()\n",
    "except ZeroDivisionError as ze:\n",
    "    print(\"Why on earth you are dividing a number by ZERO!!\")\n",
    "except:\n",
    "    print(\"Any other exception\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 52\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Define a custom exception class which takes a string message as attribute._**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints\n",
    "\n",
    "> **_To define a custom exception, we need to define a class inherited from Exception._**\n",
    "\n",
    "---\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class CustomException(Exception):\n",
    "    \"\"\"Exception raised for custom purpose\n",
    "\n",
    "    Attributes:\n",
    "        message -- explanation of the error\n",
    "    \"\"\"\n",
    "\n",
    "    def __init__(self, message):\n",
    "        self.message = message\n",
    "\n",
    "\n",
    "num = int(input())\n",
    "\n",
    "try:\n",
    "    if num < 10:\n",
    "        raise CustomException(\"Input is less than 10\")\n",
    "    elif num > 10:\n",
    "        raise CustomException(\"Input is grater than 10\")\n",
    "except CustomException as ce:\n",
    "    print(\"The error raised: \" + ce.message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 53\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Assuming that we have some email addresses in the \"username@companyname.com\" format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only._**\n",
    "\n",
    "> **_Example:\n",
    "> If the following email address is given as input to the\n",
    "> program:_**\n",
    "\n",
    "> john@google.com\n",
    "\n",
    "> **_Then, the output of the program should be:_**\n",
    "\n",
    "> john\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints\n",
    "\n",
    "> **_Use \\w to match letters._**\n",
    "\n",
    "---\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "email = \"john@google.com\"\n",
    "email = email.split(\"@\")\n",
    "print(email[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "email = \"john@google.com elise@python.com\"\n",
    "pattern = \"(\\w+)@\\w+.com\"\n",
    "ans = re.findall(pattern, email)\n",
    "print(ans)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
